#region Using directives
using System;
using System.Data;

using WP.Commons;
using WP.DB;
using WP.MF.DataEntity; 
#endregion
namespace WP.MF.DataAccess
{

	/*
	 * Created by SmartCreateCode Tool
	 * At 4/5/2005
	 */

    public class MSwitchDAL : IDataAccess
	{
		public MSwitchDAL() 
		{
		}

        #region Select List
        public string SQLList(int iParameter)
        {
            string strSql = string.Empty;
            //strSql = @"SELECT RecId,SwitchSys,SwitchName,SwitchDesc,SwitchValue,ProgressSwitchName FROM MSwitch where 1=1 " + Permission.GetSubCondition(false, "mswitch", iParameter) +
            //          " order By SwitchSys,SwitchName";
            strSql = @"SELECT RecId,SwitchSys,SwitchName,SwitchDesc,SwitchValue,NotUsed FROM MSwitch where 1=1 " + Permission.GetSubCondition(false, "mswitch", iParameter) +
                      " order By SwitchSys,SwitchName"; 
            return strSql;
        }
        #endregion
        #region Fields
        public string Fields
        {
            get
            {
                return @"SwitchSys,SwitchName,SwitchDesc,SwitchValue,ProgressSwitchName,SwitchNo,SpecialSwitch,InSwitchMaint,NotUsed";
            }
        }
        #endregion
        #region Insert MSwitch by DataRow
        public void InsertMSwitch(DBAccess dBAccess, MSwitchDEL mSwitchDEL)
        {
            string strSql = string.Empty;
            DataRow dr = mSwitchDEL.Tables[0].Rows[0];
            strSql = BaseFunctions.GenerateSqlForDataAccess(Fields, MSwitchDEL.TABLENAME_MSWITCH, dr, FormStatus.AddNew);
            dr[MSwitchDEL.RECID] = dBAccess.GetObjectValue(strSql);
            strSql = @"INSERT INTO mswitchLog  (UserName,Udate,UTime,USys,Logical,SwitchName,NewValue) 
                            VALUES ( '{0}','{1}','{2}','{3}','{4}','{5}','{6}' )";
            strSql = string.Format(strSql, SysVar.UserName,
                                           DateTime.Now.Date.ToString(),
                                           string.Format("{0:00}", DateTime.Now.Hour.ToString()) + ":" + string.Format("{0:00}", DateTime.Now.Minute.ToString()),
                                           dr[MSwitchDEL.SWITCHSYS].ToString(),
                                           1,
                                           dr[MSwitchDEL.SWITCHNAME].ToString(),
                                           (CvtFunc.ToBoolean(dr[MMiscSwitchDEL.SWITCHVALUE]) ? "1" : "0")
                                           );
            dBAccess.ExecuteSql(strSql);
        }
        #endregion
        #region Update MSwitch by DataRow
        public void UpdateMSwitch(DBAccess dBAccess, MSwitchDEL mSwitchDEL)
        {
            string strSql = string.Empty;
            DataRow dr = mSwitchDEL.Tables[0].Rows[0];
            string strOldValues = DataFunctions.GetFieldValue(dBAccess, MSwitchDEL.TABLENAME_MSWITCH, MSwitchDEL.SWITCHVALUE, "RecId =" + dr[MSwitchDEL.RECID]);
            strSql = BaseFunctions.GenerateSqlForDataAccess(Fields, MSwitchDEL.TABLENAME_MSWITCH, dr, FormStatus.Update);
            dr[MSwitchDEL.RECSTAMP] = dBAccess.GetObjectValue(strSql);
            strSql = @"INSERT INTO mswitchLog  (UserName,Udate,UTime,USys,Logical,SwitchName,OriginValue,NewValue) 
                            VALUES ( '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}' )";
            strSql = string.Format(strSql, SysVar.UserName,
                                           DateTime.Now.Date.ToString(),
                                           string.Format("{0:00}",DateTime.Now.Hour.ToString()) + ":" + string.Format("{0:00}",DateTime.Now.Minute.ToString()),
                                           dr[MSwitchDEL.SWITCHSYS].ToString(),
                                           1,
                                           dr[MSwitchDEL.SWITCHNAME].ToString(),
                                           (CvtFunc.ToBoolean(strOldValues) ? "1" : "0"),
                                           (CvtFunc.ToBoolean(dr[MMiscSwitchDEL.SWITCHVALUE])? "1" : "0")
                                           );
            dBAccess.ExecuteSql(strSql);
        }
        #endregion
        #region Delete MSwitch by Key
        public void DeleteMSwitch(DBAccess dBAccess, int iRecID)
        {
            string strSql = string.Empty;
            strSql = @"DELETE FROM mSwitch WHERE RecID=" + iRecID;
            dBAccess.ExecuteSql(strSql);
        }
        #endregion
        #region Update Post Flag
        public void UpdatePostFlag(string strRecIDs)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            try
            {
                dBAccess.ExecuteSql("update mSwitch set SwitchValue=0 where RecID in (" + strRecIDs + ")");
                dBAccess.CommitTranAndCloseCon();
            }
            catch(Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
            }
        }
        #endregion
        #region Update MSwitchRemark
        public void UpdateMSwitchRemark(DBAccess dbAccess, string strModuleName, string strSwitchName, string strLogic, string strRemark)
        {
            string strSql = string.Empty;
            strSql = "insert into mSwitchRemark (ModuleName,SwitchName,Logic,Remark) Values ('" + strModuleName + "','" + strSwitchName + "'," + strLogic + ",'" + strRemark + "')";
            dbAccess.ExecuteSql(strSql);
        }
        #endregion
        #region Delete MSwitchRemark
        public void DeleteMSwitchRemark(DBAccess dbAccess, string strModuleName, string strSwitchName)
        {
            string strSql = string.Empty;
            strSql = "delete from mSwitchRemark where ModuleName='" + strModuleName + "' and SwitchName='" + strSwitchName + "'";
            dbAccess.ExecuteSql(strSql);
        }
        #endregion
    }
}
